package 算法.线性表;

import java.util.Arrays;
import java.util.Scanner;

public class N皇后 {
    private static int max;
    private static int sum = 0;
    private static int[] array;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("输入皇后规模:");
        max = in.nextInt();
        array = new int[max];
        PutKing(0);
        System.out.println(max + "皇后的解法:" + sum);
    }

    private static void PutKing(int n) {
        if (n == max) {
            sum++;
            System.out.println(Arrays.toString(array));
            return;
        }
        for (int i = 0; i < max; i++) {
            array[n] = i;
            if (judge(n)) {
                PutKing(n + 1);
            }
        }
    }

    public static boolean judge(int n) {
        for (int i = 0; i < n; i++) {
            if (array[i] == array[n] || (Math.abs(n - i) == Math.abs(array[n] - array[i]))) {
                return false;
            }
        }
        return true;
    }

}
